﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using ASPItalia.ModelVirtualCasting.Silverlight.Data;
using GalaSoft.MvvmLight.Command;
using ASPItalia.ModelVirtualCasting.Web.RiaServices;
using GalaSoft.MvvmLight.Messaging;
using System.Collections.ObjectModel;

namespace ASPItalia.ModelVirtualCasting.Silverlight.BackOffice.ViewModels
{
  public class PeopleDetailsViewModel : ViewModelBase
  {
    DataManager _data;
    public RelayCommand<People> UpdateCommand { get; private set; }

    public PeopleDetailsViewModel()
    {
      if (DesignerProperties.IsInDesignTool)
      {
      }
      else
      {
        _data = new DataManager();
      }

      UpdateCommand = new RelayCommand<People>(Update);

      //Messenger.Default.Register<string>(this, LoadPeople);
    }
    public PeopleDetailsViewModel(People item)
      : this()
    {
      Item = item;
    }

    public void LoadMultimediaOfPeople()
    {
      Processing = Tuple.Create<bool, string>(true, "caricamento in corso");

      _data.LoadMultimediaOfPeople((result) =>
      {
        if (result.Error != null)
        {
          Processing = Tuple.Create<bool, string>(true, result.Error.Message);
        }
        else
        {
          Processing = Tuple.Create<bool, string>(false, "elaborazione completata");
          Multimedias = new ObservableCollection<Multimedia>(result.Entities);
        }
      }, Item.ID);
    }

    Tuple<bool, string> _processing;
    public Tuple<bool, string> Processing
    {
      get { return _processing; }
      set
      {
        _processing = value;
        RaisePropertyChanged("Processing");

        Messenger.Default.Send<Tuple<bool, string>>(value);
      }
    }

    private People _item = new People();
    public People Item
    {
      get { return _item; }
      set
      {
        _item = value;
        RaisePropertyChanged("Item");

        LoadMultimediaOfPeople();
      }
    }

    private ObservableCollection<Multimedia> _multimedias = new ObservableCollection<Multimedia>();
    public ObservableCollection<Multimedia> Multimedias
    {
      get { return _multimedias; }
      set
      {
        _multimedias = value;
        RaisePropertyChanged("Multimedias");
      }
    }

    public void Update(object parameter)
    {
      _data.UpdatePeople();
    }
  }
}
